package wms.handler;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import wms.entity.Operator;
import wms.util.constant.SysConstant;
import wms.util.vcode.VerifyCode;

@Controller
public class LoginController extends BaseController{
	
	
	
	@RequestMapping(value = "/login.html", method = RequestMethod.GET)
	public String getLoginPage() {
		log.debug("登录页面-------->");
		return "login";
	}
	
	@ResponseBody
	@RequestMapping(value = "/dologin")
	public Map<String, String> doLogin(@RequestParam("loginCode")String loginCode, @RequestParam("password") String password, 
									@RequestParam("vcode") String vcode, HttpSession session) throws Exception {
		log.debug("登录验证用户------>" + loginCode);
		Map<String, String> map = new HashMap<String, String>();
		
		//验证码校验
		if (!getRedisApi().keyExist(SysConstant.VCODE)) {
			map.put(SysConstant.MSG, SysConstant.VCODE_INVIDLE);
		} else {
			String validateCode = getRedisApi().getStringKey(SysConstant.VCODE);
			System.out.println("reids的验证码:" + validateCode + "\t表单验证码:" + vcode);
			if (vcode.equalsIgnoreCase(validateCode)) {
				//登录校验
				Operator operator = getOperatorService().login(loginCode, password);
				if (operator == null) {
					map.put(SysConstant.MSG, SysConstant.LOGINCODE_NOT_EXIST);
				} else {
					//登录成功  
					//1.增加session
					//2.修改登录时间
					System.out.println("登录成功.......");
					if (getOperatorService().modifyOperator(operator)) {
						session.setAttribute(SysConstant.OPERATOR, operator);
						map.put(SysConstant.MSG, SysConstant.SUCCESS);
					}
				}
			} else {
				map.put(SysConstant.MSG, SysConstant.VCODE_ERROR);
			}
		}
		
		return map;
	}
	
	/**
	 * 生成验证码
	 * @param response
	 * @param session
	 * @throws Exception
	 */
	@RequestMapping(value = "verifycode.html", method=RequestMethod.GET)
	public void verifyCode(HttpServletResponse response, HttpSession session) throws Exception {
		log.debug("获取验证码中...........");
		//设置浏览器不缓存
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		//将验证码输出到客户端
		response.setContentType("image/jpeg");

		String code = VerifyCode.generateVerifyCode(4);
		//3分钟有效期
		getRedisApi().set(SysConstant.VCODE, code, 180);
		
		
		ServletOutputStream outputStream = response.getOutputStream();
		try{
			VerifyCode.outputImage(105, 30, outputStream, code);
		} catch (Exception e) {
			System.err.println("验证码获取异常");
			throw new RuntimeException(SysConstant.ERROR_MSG);
		} finally {
			if (outputStream != null) {
				outputStream.flush();
				outputStream.close();
			}
		}
		
	}
	
	
	/**
	 * 退出
	 * @return
	 */
	@RequestMapping(value ="/loginout.html", method = RequestMethod.GET)
	public String loginOut(HttpSession session){
		log.debug("用户注销---->");
		session.removeAttribute(SysConstant.OPERATOR);
		session.invalidate();
		return "redirect:/login.html";
	}
	
}
